#include <iostream>
using namespace std;

class Student{
    private:
        string name;
        int age;
    public:
        Student(){
            cout << this << " 无参构造函数" << endl;
        }
        Student(string n, int a):name(n), age(a){
            cout << this << " 有参构造函数" << endl;
        }
        Student(const Student &other):name(other.name), age(other.age){
            cout << this << " 拷贝构造函数" << endl;
        }
        ~Student(){
            cout << this << " 析构函数" << endl;
        }
        void show(){
            cout << name << " " << age << endl;
        }
};

void func(Student x){
    cout << "func : ";
    x.show();
}

int main(int argc, const char *argv[]){
    Student s1("小明", 18); // 有参构造
    s1.show();

    // 理论上：此处会调用 匿名对象的有参构造函数 然后调用 s2的拷贝构造函数
    // 有些编译器会对代码做优化 只调用了s2的有参构造
    // 可以使用编译选项 g++ xxx.cpp  -fno-elide-constructors 来防止编译器优化 看到现象
    Student s2 = Student("小红", 20);
    cout << &s2 << endl;

    cout << "---------------------" << endl;
    // 匿名对象一般多用于类数组的初始化
    Student arr[3] = {Student("张三", 15), Student("李四", 18), Student("王五", 17)};
    cout << "---------------------" << endl;

    // 也可以用于函数传参
    func(Student("小强", 30));

    return 0;
}